package com.cait.dutyfree.action.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.cait.dutyfree.cart.CartProcessor;
import com.cait.dutyfree.dataaccess.ProductServiceDAO;
import com.cait.dutyfree.pojo.FlightAndPassportDetail;
import com.cait.dutyfree.pojo.Product;
import com.cait.dutyfree.pojo.ShoppingCartItem;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.conversion.annotations.TypeConversion;

public class ShoppingCartAction extends ActionSupport implements
		ServletContextAware, SessionAware, ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String viewCart() {
		List<ShoppingCartItem> items = this.cartProcessor
				.getAllCartItems(this.session);
		this.setProducts(items);
		return SUCCESS;
	}

	public String addToCart() {
		int productId = Integer.valueOf(request.getParameter("productId"));
		int quantity = Integer.valueOf(request.getParameter("quantity"));
		// String passportNo = request.getParameter("passportNo");
		int airlineId = Integer.valueOf(request.getParameter("airlineId"));

		Product p = productServiceDAO.getById(productId);

		boolean b = productServiceDAO.isProductAvailableForAirline(airlineId,
				productId);
		boolean isEnoughStockAvailable = this.cartProcessor
				.isEnoughStockAvailable(p, session, quantity);

		if (!b) {
			request.setAttribute("errorMsg", "Not available for you airline");
			return Action.ERROR;
		} else if (!isEnoughStockAvailable) {
			request.setAttribute("errorMsg", "Not enough stock");
			return Action.ERROR;
		} else {
			this.cartProcessor.addToCart(p, quantity, this.session);
			if (!this.session.containsKey("airlineId")) {
				this.session.put("airlineId", new Integer(airlineId));
			}
			return SUCCESS;
		}
	}

	public String removeFromCart() {
		int id = Integer.valueOf(request.getParameter("id"));
		Product p = productServiceDAO.getById(id);
		System.out.println("removing from shopping cart");
		this.cartProcessor.removeFromCart(p, this.session);
		return SUCCESS;
	}

	public String updateCart() {
		int id = Integer.valueOf(request.getParameter("id"));
		int count = Integer.valueOf(request.getParameter("count"));
		Product p = productServiceDAO.getById(id);
		System.out.println("updating");
		this.cartProcessor.updateCart(p, session, count);
		return SUCCESS;
	}

	public String checkout() {
		return SUCCESS;
	}

	public String flightDetails() {
		return SUCCESS;
	}

	public String submitFlightDetails() {
		// validations
		if (this.getFlightNumber() == null || this.getFlightNumber().isEmpty()) {
			this.addFieldError("flightNumber", "Flight number is required");
		}
		if (this.getFlightDate() == null) {
			this.addFieldError("flightDate", "Invalid flight date");
		}
		if (this.getPassportNumber() == null
				|| this.getPassportNumber().isEmpty()) {
			this.addFieldError("passportNumber", "Invalid passport number");
		}
		if (this.getFromDestination() == null
				|| this.getFromDestination().isEmpty()) {
			this.addFieldError("fromDestination", "Invalid from destination");
		}
		if (this.getToDestination() == null
				|| this.getToDestination().isEmpty()) {
			this.addFieldError("toDestination", "Invalid to destination");
		}
		if (this.getShippingDate() == null) {
			this.addFieldError("shippingDate", "Invalid shipping date");
		}
		if (this.hasErrors()) {
			return INPUT;
		} else {
			FlightAndPassportDetail fapd = new FlightAndPassportDetail();
			fapd.setFlightNumber(this.getFlightNumber());
			fapd.setFromDestination(this.getFromDestination());
			fapd.setPassportNumber(this.getPassportNumber());
			fapd.setToDestination(this.getToDestination());
			fapd.setFlightDate(this.getFlightDate());
			fapd.setShippingDate(this.getShippingDate());

			return SUCCESS;
		}

	}

	public String creditCartSummit() {
		if (this.getNameOnCard() == null || this.getNameOnCard().isEmpty()) {
			this.addFieldError("NameOnCard", "Name on card is required");
		}
		if (this.getCardNumber() == null || this.getCardNumber().isEmpty()) {
			this.addFieldError("cardNumber", "Card number is required");
		}
		if (this.getCvcNo() == null || this.getCvcNo().isEmpty()) {
			this.addFieldError("cvcNo", "CVV number is required");
		}
		if (this.getBillingAddress() == null
				|| this.getBillingAddress().isEmpty()) {
			this.addFieldError("billingAddress", "Billing address is required");
		}
		if (this.hasErrors()) {
			return INPUT;
		} else {
			return SUCCESS;
		}

	}

	// private methods used in this class only
	private ServletContext servletContext;
	private HttpServletRequest request;
	private Map<String, Object> session;
	private CartProcessor cartProcessor;
	private ProductServiceDAO productServiceDAO;
	private List<ShoppingCartItem> products;

	// form variables for submitFlightDetails
	private String flightNumber;
	private Date flightDate;
	private String passportNumber;
	private String fromDestination;
	private String toDestination;
	private Date shippingDate;
	// end form variables for submitFlightDetails

	// form variables for creditCartSummit
	private String NameOnCard;
	private String cardNumber;
	private String expDateYear;
	private String expDateMonth;
	private String cvcNo;
	private String billingAddress;
	// form variables for creditCartSubmit here

	// chekcout
	private ArrayList<String> expYears = new ArrayList<String>();
	private ArrayList<String> expMonths = new ArrayList<String>();

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.request = arg0;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	@Override
	public void setServletContext(ServletContext arg0) {
		this.servletContext = arg0;
	}

	public void setCartProcessor(CartProcessor cartProcessor) {
		this.cartProcessor = cartProcessor;
	}

	public void setProductServiceDAO(ProductServiceDAO productServiceDAO) {
		this.productServiceDAO = productServiceDAO;
	}

	public void setProducts(List<ShoppingCartItem> products) {
		this.products = products;
	}

	public List<ShoppingCartItem> getProducts() {
		return products;
	}

	public String getFlightNumber() {
		return flightNumber;
	}

	public void setFlightNumber(String flightNumber) {
		this.flightNumber = flightNumber;
	}

	public Date getFlightDate() {
		return flightDate;
	}

	@TypeConversion(converter = "com.cait.dutyfree.action.converter.DatetimeConverter")
	public void setFlightDate(Date flightDate) {
		this.flightDate = flightDate;
	}

	public String getPassportNumber() {
		return passportNumber;
	}

	public void setPassportNumber(String passportNumber) {
		this.passportNumber = passportNumber;
	}

	public String getFromDestination() {
		return fromDestination;
	}

	public void setFromDestination(String fromDestination) {
		this.fromDestination = fromDestination;
	}

	public String getToDestination() {
		return toDestination;
	}

	public void setToDestination(String toDestination) {
		this.toDestination = toDestination;
	}

	public Date getShippingDate() {
		return shippingDate;
	}

	@TypeConversion(converter = "com.cait.dutyfree.action.converter.DatetimeConverter")
	public void setShippingDate(Date shippingDate) {
		this.shippingDate = shippingDate;
	}

	public ArrayList<String> getExpYears() {
		Calendar c = Calendar.getInstance();
		for (int i = c.get(Calendar.YEAR); i < c.get(Calendar.YEAR) + 5; i++) {
			expYears.add(String.valueOf(i));
		}
		for (int i = 0; i < expYears.size(); i++) {
			System.out.println("years:" + expYears.get(i));
		}
		return expYears;
	}

	public void setExpYears(ArrayList<String> expYears) {
		this.expYears = expYears;
	}

	public String getExpDateYear() {
		return expDateYear;
	}

	public void setExpDateYear(String expDateYear) {
		this.expDateYear = expDateYear;
	}

	public ArrayList<String> getExpMonths() {
		for (int i = 1; i < 13; i++) {
			expMonths.add(String.valueOf(i));
		}
		return expMonths;
	}

	public void setExpMonths(ArrayList<String> expMonths) {
		this.expMonths = expMonths;
	}

	public String getNameOnCard() {
		return NameOnCard;
	}

	public void setNameOnCard(String nameOnCard) {
		NameOnCard = nameOnCard;
	}

	public String getCardNumber() {
		return cardNumber;
	}

	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}

	public String getExpDateMonth() {
		return expDateMonth;
	}

	public void setExpDateMonth(String expDateMonth) {
		this.expDateMonth = expDateMonth;
	}

	public String getCvcNo() {
		return cvcNo;
	}

	public void setCvcNo(String cvcNo) {
		this.cvcNo = cvcNo;
	}

	public String getBillingAddress() {
		return billingAddress;
	}

	public void setBillingAddress(String billingAddress) {
		this.billingAddress = billingAddress;
	}

}
